<template>
	<view class="page">
		<status-bar :title='title'></status-bar>
		<backTop v-if='showBackTop'></backTop>
		<view class="topBarBox" v-if='type == 1'>
			<view class="topBar flex-box" :style="{top: navHeight+System.statusBarHeight+'px'}">
				<view class="flex" @click="check(0)" :class="{active:currIndex == 0}" :style="{'--color': color}">学习中({{nums[0]}})</view>
				<view class="flex" @click="check(1)" :class="{active:currIndex == 1}" :style="{'--color': color}">已过期({{nums[1]}})</view>
				<view class="bar" :style="{left:currIndex*50+'%','--color': color}"></view>
			</view>
		</view>
		
		<view class="list" v-if='type == 1'>
			<view v-for="(item,index) in lessonList" :key='index' @click="toDetail(item.lesson_id)" class="lesssonItem">
				<view class="flex">
					<view class="lesson-title lesson-title-sm">{{item.lesson_title}}</view>
					<view class="lesson-info">{{item.lesson_info}}</view>
					<view class="lesson-info">有效时间 {{item.left_day}}天</view>
					<view class="tags">
						<text class="gray">上次学习时间 {{item.last_study_time}}</text>
						<text class="gray">已学习{{item.study_times}}次</text>
					</view>
				</view>
			</view>
		</view>
		
		<view class="list" v-if='type == 2'>
			<view class="list-item" v-for="(item1,index1) in classList" :key='index1'>
				<view class="list-title" :style="{'--color': color}">{{ item1.class_name }}</view>
				<view v-for="(item,index) in item1.lessons" :key='index' @click="toDetail(item.lesson_id)" class="lesssonItem">
					<view class="flex">
						<view class="lesson-title lesson-title-sm">{{item.lesson_title}}</view>
						<view class="lesson-info">{{item.lesson_info}}</view>
						<view class="lesson-info">有效时间 {{item.left_day}}天</view>
						<view class="tags">
							<text class="gray">上次学习时间 {{item.last_study_time}}</text>
							<text class="gray">已学习{{item.study_times}}次</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<empty v-if='noData' cat='3' msg='没有相关课程哦 !'></empty>
	</view>
</template>

<script>
	let that;
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				
				title:' ',
				currIndex: 0,
				class_id: '',
				nums: [0, 0],
				lessonList: '',
				classList: '',
				noData: false,
				showBackTop: false,
				staticUrl: this.staticUrl,
				imgUrl: this.imgUrl,
				color: getApp().globalData.color,
				type: 1,
			}
		},
		onPageScroll(e) {
			if (e.scrollTop > 500) {
				this.showBackTop = true
			} else {
				this.showBackTop = false
			}
		},
		onLoad(e) {
			that = this
			that.class_id = e.cid;
			if(e.type){
				that.type = e.type
			}
			if(that.type == 1){
				that.getHomeData()
			}
			if(that.type == 2){
				that.get_my_class(e.status)
				if(e.status == 1){
					that.title = '学习中'
				}
				if(e.status == 2){
					that.title = '已过期'
				}
				uni.setNavigationBarTitle({
					title: that.title
				})
			}
			
		},
		methods: {
			check(index) {
				that.currIndex = index;
				that.getHomeData()
			},
			get_my_class(order_status) {
				that.noData = false;
				that.$http.get({
					url: '/zzj_singleSaleApi/my_class',
					data: {
						bid: uni.getStorageSync('bid'),
						v_id: uni.getStorageSync('vid'),
						order_status: order_status
					}
				}).then(res => {
					if (res.code == 100 && res.data) {
						that.classList = res.data
					}
					if (!that.classList || that.classList.length < 1) {
						that.noData = true
					}
				})
			},
			getHomeData() {
				that.noData = false;
				that.lessonList = '';
				uni.showLoading({
					title: '加载中'
				})
				that.$http.get({
					url: '/zzj_singleSaleApi/my_lesson',
					data: {
						bid: uni.getStorageSync('bid'),
						v_id: uni.getStorageSync('vid'),
						class_id: that.class_id
					}
				}).then(res => {
					if(res.data.class_name){
						uni.setNavigationBarTitle({
							title: res.data.class_name
						})
						that.title = res.data.class_name
					}
					if (res.code == 100) {
						that.nums[0] = res.data.lesson[0].num
						that.nums[1] = res.data.lesson[1].num
						if (that.currIndex == 0) {
							that.lessonList = res.data.lesson[0].lessons
						} else {
							that.lessonList = res.data.lesson[1].lessons
						}
					}
					if (!that.lessonList || that.lessonList.length < 1) {
						that.noData = true
					}
					uni.hideLoading()
				})
			},
			toDetail(id){
				let bid = uni.getStorageSync('bid');
				// #ifdef H5
				window.location.href = 'https://sra.weikebaba.com/index.php/singleSaleLesson/index?veriCode='+bid+'&pageType=1&id='+id
				// #endif
			}
		},
		computed:{
			...mapState(['tabTitle','color','city','navHeight','System','share']),
		},
	}
</script>

<style lang="scss" scoped>
	.page {
		.topBarBox{
			height: 88rpx;
			.topBar {
				position: fixed;
				top: 0;
				left: 0;
				right: 0;
				height: 88rpx;
				background-color: #FFFFFF;
				box-shadow: 0 0 3px 0 rgba(0, 0, 0, .1);
				z-index: 33;
				view {
					text-align: center;
					height: 100%;
					font-size: 28rpx;
					font-weight: bold;
					line-height: 88rpx;
				}
				.bar {
					width: 80rpx;
					height: 4rpx;
					left: 0;
					background-color: var(--color);
					position: absolute;
					bottom: 0;
					border-radius: 4rpx;
					margin-left: 140rpx;
					transition: all .2s;
				}
			
				.active {
					color: var(--color);
				}
			}
		}
		.list {
			padding: $uni-page-space;
			.list-title{
				font-weight: bold;
				font-size: 34rpx;
				padding-bottom: 20rpx;
				position: relative;
				&::before{
					content: '';
					position: absolute;
					width: 8rpx;
					height: 34rpx;
					border-radius: 0 4rpx 4rpx 0;
					top: 8rpx;
					background-color: var(--color);
					left: -$uni-page-space;
				}
			}
			.lesssonItem{
				padding: $uni-page-space;
				background-color: #FFFFFF;
				border-radius: 8rpx;
				box-shadow:  0 0 3px 0 rgba(0,0,0,.1);
				margin-bottom: $uni-page-space;
				display: flex;
				position: relative;
				.lessonImg{
					width: 200rpx;
					height: 200rpx;
					margin-right: $uni-page-space;
				}
				.tags{
					margin-top: 20rpx;
					text{
						margin-right: 10rpx;
						line-height: 36rpx;
						border-radius: 36rpx;
						height: 36rpx;
						display: inline-block;
						font-size: 24rpx;
						padding: 0 16rpx;
					}
					.green{	
						background-color: #E3FBF0;
						color: #01D475;
					}
					.yellow{
						background-color: #FEF7EB;
						color: #F8AD24;
					}
					.red{
						background-color: #FEEFEF;
						color: #DE5858;
					}
					.gray{
						background-color: #EEEEEE;
						color: #666666;
					}
					.price {
						padding: 0;
						margin-right: 0;
						color: #FF0000;
						font-size: 34rpx;
						font-weight: bold;
					}
				}
				.tag{
					position: absolute;
					width: 24rpx;
					top: 0;
					height: 20rpx;
					right: 20rpx;
					background-color: $tag-bg;
					border-radius: 0 0 8rpx 8rpx;
					margin-bottom: 20rpx;
				}
				.lesson-title{
					font-size: 34rpx;
					font-weight: bold;
					margin-bottom: 20rpx;
					line-height: 48rpx;
					text-align: justify;
				}
				.lesson-title-sm{
					font-size: 28rpx;
				}
				.lesson-info{
					font-size: 24rpx;
					color: #666666;
					text-align: justify;
					margin-bottom: 16rpx;
				}
				
				// 学习中
				.study{
					padding-right: 70rpx;
				}
				.studying{
					color: #01D475;
					background-color: #E3FBF0;
					line-height: 36rpx;
					font-size: 24rpx;
					height: 36rpx;
					display: block;
					padding: 0 4rpx 0 12rpx;
					border-radius: 32rpx 0 0 32rpx;
					position: absolute;
					right: 0;
					top: 20rpx;
				}
			}
		}
	}
</style>
